package com.springcloud.filter.pre;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

public class ChangeRequestZuulFilter extends ZuulFilter {

	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		String requestURI = request.getRequestURI();
		// 放行，不拦截静态资源
		if (requestURI.endsWith(".html") || requestURI.endsWith(".jsp") || requestURI.endsWith(".js")
				|| requestURI.endsWith(".css")) {
			return false;
		}
		return true;
	}

	@Override
	public Object run() {

		// MultipartHttpServletRequest multiPartRequest = new
		// DefaultMultipartHttpServletRequest(req);

		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();

		CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart(request);
		ctx.setRequest(multipartRequest);
		return null;
	}

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return -9999;
	}

}
